<template>
  <div>
    <el-form :inline="true">
      <el-form-item>
        <el-button type="primary"
                   icon="el-icon-plus" @click="handleAdd">
          添加
        </el-button>
      </el-form-item>
    </el-form>
    <el-table
      border
      :data="tableData"
      style="width: 100%">
      <el-table-column
        prop="id"
        width="50"
        label="id">
      </el-table-column>
      <el-table-column
        prop="title"
        label="活动标题">
      </el-table-column>
      <el-table-column
        prop="startTime"
        label="开始时间">
      </el-table-column>
      <el-table-column
        prop="endTime"
        label="结束时间">
      </el-table-column>
      <el-table-column
        prop="place"
        label="活动地点">
      </el-table-column>
      <el-table-column
        prop="courseIntroduction"
        label="封面图">
        <template slot-scope="{row}">
          <img width="60" :src="row.coverPlan" alt="">
        </template>
      </el-table-column>
      <el-table-column
        label="操作">
        <template slot-scope="{row}">
          <el-button
            size="mini"
            type="success"
            @click="update(row)">编辑</el-button>
          <el-button
            size="mini"
            type="danger"
            @click="del(row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-dialog
      width="70%"
      style="z-index: 1"
      :modal="false"
      @close="diaClose"
      :visible.sync="dialogFormVisible">
      <el-form ref="ff" label-width="88px" :model="form">
        <el-form-item label="活动标题">
          <el-input v-model="form.title" />
        </el-form-item>
        <el-form-item label="活动地点">
          <el-input v-model="form.place" />
        </el-form-item>
        <el-form-item label="活动标签">
          <el-input v-model="form.label" />
        </el-form-item>
        <el-form-item label="封面">
          <el-upload
            class="avatar-uploader"
            action="http://47.101.57.131:8101/common/api/file/upload2"
            :show-file-list="false"
            :on-success="handleAvatarSuccess">
            <img v-if="form.coverPlan" :src="form.coverPlan" class="avatar"  alt=""/>
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload>
        </el-form-item>
        <el-form-item label="开始时间">
          <el-date-picker
            v-model="form.startTime"
            type="datetime"
            value-format="yyyy-MM-dd hh:mm:ss"
            placeholder="选择日期时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="结束时间">
          <el-date-picker
            v-model="form.endTime"
            type="datetime"
            value-format="yyyy-MM-dd hh:mm:ss"
            placeholder="选择日期时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="内容">
          <tinymce-component v-model="form.content" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submit">确 定</el-button>
      </div>
    </el-dialog>
    <el-pagination
      background
      :current-page="current"
      style="margin-top: 20px;"
      @current-change="currentChange"
      layout="prev, pager, next"
      :total="total">
    </el-pagination>
  </div>
</template>

<script>
import { getHd,addHd,editNK ,delHd} from "@/api/ziyuan";
import {deepClone} from "@/utils";
import tinymceComponent from '@/components/tinymce'


export default {
  name: "index",
  components:{
    tinymceComponent
  },
  data() {
    return {
      current:1,
      tableData:[],
      total:0,
      dialogFormVisible:false,
      bianji:false,
      form:{
        "title": "",
        "startTime": "",
        "endTime": "",
        "coverPlan": "",
        "fileId": 0,
        "content": "",
        "place":"",
        "label":""
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    getList(){
      getHd(this.current).then(res=>{
        console.log(res.data)
        this.tableData = res.data.records
        this.total = res.data.total
        this.current = res.data.current
      })
    },
    del(row){
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        const res = await delHd({
          id:row.id,
          fileId:row.fileId
        })
        console.log(res)
        this.$message.success('操作成功')
        this.getList()
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });
      });
    },
    update(row){
      this.bianji = true
      this.form = row
      this.dialogFormVisible = true
    },
    currentChange(page){
      this.current = page
      this.getList()
    },
    async submit(){
      console.log(this.form)
      if (this.bianji){
        await editNK(this.form)
      }else {
        const res = await addHd(this.form)
        console.log(res)
      }
      this.$message.success('操作成功！')
      this.dialogFormVisible = false
      this.getList()
    },
    handleAdd(){
      this.bianji = false
      this.dialogFormVisible = true
    },
    diaClose(){
      this.$refs.ff.resetFields()
      this.form = {
        content: ""
      }
    },
    views(){

    },
    handleAvatarSuccess(res){
      console.log(res)
      this.form.coverPlan = res.data.fileUrl
      this.form.fileId = res.data.id
    }
  }
}
</script>

<style scoped>
>>>.el-dialog__wrapper{
  z-index: 999 !important;
}
</style>
